package org.jeecg.modules.orderForm.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.tool.hbm2ddl.SingleLineSqlCommandExtractor;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.handle.ISendMsgHandle;
import org.jeecg.modules.message.handle.enums.SendMsgStatusEnum;
import org.jeecg.modules.message.handle.enums.SendMsgTypeEnum;
import org.jeecg.modules.message.service.ISysMessageService;
import org.jeecg.modules.orderForm.entity.OrderForm;
import org.jeecg.modules.orderForm.service.IOrderFormService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 发送消息任务
 */

@Slf4j
public class OrderFormJob implements Job {
	/**
	 * 若参数变量名修改 QuartzJobController中也需对应修改
	 */
	private String parameter;

	public void setParameter(String parameter) {
		this.parameter = parameter;
	}
	@Autowired
	private IOrderFormService orderFormService;

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		log.info(String.format("订单修改状态 发送消息任务 OrderFormJob !  时间:" + DateUtils.getTimestamp()));
		String[] str=this.parameter.split("-");
		//System.err.println("str-->"+str[0]);
		//System.err.println("str-->"+str[1]);
		QueryWrapper<OrderForm> queryWrapper = new QueryWrapper<OrderForm>();
		queryWrapper.eq("order_type",0);
		queryWrapper.eq("order_status",3);
		queryWrapper.last("and DATE_SUB(CURDATE(), INTERVAL "+str[0]+" DAY) >= create_time");
		List<OrderForm> OrderFormlist=orderFormService.list(queryWrapper);

		for(OrderForm orderForm:OrderFormlist){
			orderForm.setSettlementStatus(3);
		}
		if(OrderFormlist.size()>0){
			orderFormService.updateBatchById(OrderFormlist);
		}
		List<OrderForm> takeOrdersMoneylist=orderFormService.getGroupBytakeOrdersMoney(Integer.parseInt(str[1]));
		for(OrderForm orderForm:takeOrdersMoneylist){
				orderForm.setSettlementStatus(3);
		}
		if(takeOrdersMoneylist.size()>0){
			orderFormService.updateBatchById(takeOrdersMoneylist);
		}

	}

}
